package cn.didi.feidi.androidorder.fragment;

import android.databinding.DataBindingUtil;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import java.util.ArrayList;

import cn.didi.feidi.androidorder.R;
import cn.didi.feidi.androidorder.adapter.MainViewPagerAdapter;
import cn.didi.feidi.androidorder.databinding.FragmentMainBinding;
import cn.didi.feidi.androidorder.fragment.personal.PersonCenterFragment;

import static cn.didi.feidi.androidorder.assembly.FeiDiBottomNavigation.HOME_PAGE;
import static cn.didi.feidi.androidorder.assembly.FeiDiBottomNavigation.ORDER;
import static cn.didi.feidi.androidorder.assembly.FeiDiBottomNavigation.PERSONAL;
import static cn.didi.feidi.androidorder.assembly.FeiDiBottomNavigation.SHOPPING_CAR;

/**
 * Created by jinxiong on 2017/8/19.
 */

public class MainFragment extends FeidiBaseFragment {


    FragmentMainBinding binding;
    ArrayList<FeiDiMainBaseFragment> fragments;
    MainViewPagerAdapter adapter;

    /**
     * 与activity关联，后面可考虑单例模式
     *
     * @param bundle
     * @return
     */
    public static MainFragment newInstance(Bundle bundle) {
        MainFragment fragment = new MainFragment();
        if (bundle != null)
            fragment.setArguments(bundle);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        fragments = new ArrayList<>();

        fragments.add(new HomePageFragment());
        fragments.add(new OrderFragment());
        fragments.add(new ShoppingCarFragment());
        fragments.add(new PersonCenterFragment());

        adapter = new MainViewPagerAdapter(getChildFragmentManager(), fragments);
    }

    @Override
    protected View initFragment(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

        binding = DataBindingUtil.inflate(inflater, R.layout.fragment_main, container, false);
        return binding.getRoot();

    }

    @Override
    protected void initViews(View rootView) {
        binding.mainViewPager.setAdapter(adapter);
        fragments.get(0).actionBarSetting(getFeidiActionBar());


    }

    @Override
    protected void initData() {

    }

    @Override
    protected void addListener() {

        binding.mainBottomNavigation.setSelectEvent(HOME_PAGE, new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                selectNavigationAndChangeActionBar(HOME_PAGE);
            }
        });


        binding.mainBottomNavigation.setSelectEvent(ORDER, new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                selectNavigationAndChangeActionBar(ORDER);

            }
        });

        binding.mainBottomNavigation.setSelectEvent(SHOPPING_CAR, new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                selectNavigationAndChangeActionBar(SHOPPING_CAR);
            }
        });

        binding.mainBottomNavigation.setSelectEvent(PERSONAL, new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                selectNavigationAndChangeActionBar(PERSONAL);
            }
        });


    }

    private void selectNavigationAndChangeActionBar(byte index) {


        fragments.get(index).actionBarSetting(getFeidiActionBar());
        fragments.get(index).clearSwipeRefreshLayout();
        binding.mainViewPager.setCurrentItem(index);

    }
}
